﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DMU;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;

namespace DMU.Gui
{
    public partial class ucMonHoc : DevExpress.XtraEditors.XtraUserControl
    {
        public DataTable dtMH;
        public bool them;

        help hp = new help();
        Service sv = new Service();

        DevExpress.XtraTab.XtraTabControl tab;

        public ucMonHoc(DevExpress.XtraTab.XtraTabControl tab)
        {
            InitializeComponent();
            this.tab = tab;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {

        }

        private void ucMonHoc_Load(object sender, EventArgs e)
        {
            #region
            dtMH = new DataTable();

            DataColumn[] cl = new DataColumn[7];
            cl[0] = new DataColumn("MaMH", Type.GetType("System.String"));
            cl[1] = new DataColumn("TenMH", Type.GetType("System.String"));
            cl[2] = new DataColumn("SoTiet", Type.GetType("System.String"));
            cl[3] = new DataColumn("isTC", Type.GetType("System.Boolean"));
            cl[4] = new DataColumn("MaNganh", Type.GetType("System.String"));
            cl[5] = new DataColumn("IsActive", Type.GetType("System.Boolean"));
            cl[6] = new DataColumn("GhiChu", Type.GetType("System.String"));
            
            dtMH.Columns.AddRange(cl);
            DataColumn[] c = new DataColumn[1];
            c[0] = dtMH.Columns["MaMH"];
            dtMH.PrimaryKey = c;

            gridControl1.DataSource = dtMH;
            #endregion
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            //int rowHandle = gridView2.FocusedRowHandle;
            //if (rowHandle >= 0)
            //{
            //    if (gridView2.GetRowCellValue(rowHandle, "MaMH") != null)
            //    {
            //        them = false;
            //        GuiPro.frmThemNCC frm = new GuiPro.frmThemNCC(this);

            //        frm.txtMa.Text = gridView2.GetRowCellValue(rowHandle, "MaMH").ToString();
            //        frm.txtTen.Text = gridView2.GetRowCellValue(rowHandle, "TenMH").ToString();
            //        frm.txtdc.Text = gridView2.GetRowCellValue(rowHandle, "SoTiet").ToString();
            //        try
            //        {
            //            frm.chkTinChi.Checked = bool.Parse(gridView2.GetRowCellValue(rowHandle, "isTC").ToString());
            //        }
            //        catch (Exception t) { MessageBox.Show(t.Message); }

            //        frm.txtSDT.Text = gridView2.GetRowCellValue(rowHandle, "MaNganh").ToString();
            //        frm.txtFax.Text = gridView2.GetRowCellValue(rowHandle, "GhiChu").ToString();
            //        frm.chkActive.Checked = bool.Parse(gridView2.GetRowCellValue(rowHandle, "IsActive").ToString());

            //        frm.Text = "Bảng cập nhật môn học";
            //        frm.txtMa.Properties.ReadOnly = true;
            //        frm.Size = new System.Drawing.Size(538, 318);
            //        frm.ShowDialog();
            //    }
            //}
            //else
            //    hp.showMes("Chọn dòng để thực hiện chức năng này !", -1);
        }

        private void gridView2_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.Clicks == 2)
            {
                EventArgs et = new EventArgs();
                btnSua_Click(sender, et);
            }
        }


        private void gridView2_CustomUnboundColumnData_1(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.IsGetData)
                e.Value = e.ListSourceRowIndex + 1;
        }

        private void btnF_Click(object sender, EventArgs e)
        {
            dtMH = sv.getDatatable("dmu_proMFindToTenMH", new SqlParameter("TenMH0", txtF.Text + "%"));
            gridControl1.DataSource = dtMH;
        }

        private void txtF_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                EventArgs et = new EventArgs();
                btnF_Click(sender, et);
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            int[] rowSelec = gridView2.GetSelectedRows(); int dem = 0; int len = rowSelec.Length;
            if (len > 0)
            {
                try
                {
                    if (hp.showMes(len + " đối tượng sẽ bị xóa ?", 2) == DialogResult.Yes)
                    {
                        sv.openConnect();
                        for (int i = 0; i < len; i++)
                        {
                            if (gridView2.GetRowCellValue(rowSelec[i], "MaMH") != null)
                            {
                                string a=sv.cusTomExecuteScalarStore("dmu_proMXoaMH", new SqlParameter("MaMH0", gridView2.GetRowCellValue(rowSelec[i], "MaMH").ToString()));
                                
                                if (a!="1")
                                {
                                    gridView2.UnselectRow(rowSelec[i]);
                                    dem++;
                                }
                            }
                        }
                        sv.closeConnect();
                        gridView2.DeleteSelectedRows();
                        
                        if (dem > 0)
                            hp.showMes(dem + " đối tượng không thể xóa !", 1);
                        else
                            hp.showMes("Xóa thành công " + len + " đối tượng", -1);
                    }
                }
                catch (Exception et)
                {
                    hp.showMes("Có lỗi khi xóa. Vui lòng thử lại ! " + et.Message, 1);
                }
            }
            else
            {
                hp.showMes("Chọn dòng để thực hiện chức năng này !!", -1);
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            tab.TabPages.RemoveAt(tab.SelectedTabPageIndex);
        }

        private void gridControl1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                EventArgs et = new EventArgs();
                btnXoa_Click(sender, et);
            }

        }



    }
}
